<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Comment extends CI_Controller {
  function __construct() {
    session_start();
    parent::__construct();
    header('Content-Type: application/json');
    if(empty($_SESSION['login'])) {
      response('{status:"error",data:"Unauthorized"}', 401);
    }
    $this->load->model('MComment', 'mcomment');
  }

  function index($hash) {
    switch($_SERVER['REQUEST_METHOD']) {
      case 'GET':
      case 'get':
        $this->get($hash);
        break;
      case 'POST':
      case 'post':
        $this->post($hash);
        break;
      case 'DELETE':
      case 'delete':
        $this->delete($hash);
      default:
        response('{status:"error",data:"Method not support!"}', 405);
    }
  }

  /* task hash*/
  private function get($hash) {
    echo json_encode($this->mcomment->get($hash));
  }

  /* task hash */
  private function post($hash) {
    /* Lazy to query task id and project identity */
    if(!isset($_POST['taskId'], $_POST['projectHash'], $_POST['author'], $_POST['content'])) {
      response('{status:"error",data:"Invalid data field!"}', 406);
    }
    echo json_encode($this->mcomment->add($_POST['taskId'], $hash, $_POST['projectHash'], $_POST['author'], $_POST['content']));
  }

  /* comment hash */
  private function delete($hash) {
    echo json_encode($this->mcomment->delete($hash));
  }
}
